#coding:utf-8
'''
create on 2017-04-20
@author:sandy
'''
import logging
from pycommon import Const
from pycommon import Pack as packet
import ClientFactory
import CounterManager
import ClientBuild

def cmdKeepAlive(center,pkt):
    logging.debug(u"center--->gate keepAlive")
    pass

def cmdLoginCenterSuc(center,pkt):
    gameInfo = packet.unpack("S",pkt)
    logging.debug(u"gameInfo1 = %s",gameInfo)
    pass

def cmdGameUpdate(center,pkt):
    gameInfo = packet.unpack("S",pkt)
    logging.debug(u"gameInfo2 = %s",gameInfo)

def cmdTransPkt(center,pkt):
    uid,pkt = packet.unpack("Hs",pkt)
    logging.debug(u"Gate Trans Center Pkt To Client uid = %d",uid)
    client = ClientFactory.instance.getClientSrvByUid(uid)
    if client:
        client.sendPacket(pkt)
    else:
        logging.debug(u"cmdTransPkt client is None")


def cmdKickUser(center,pkt):
    uid,pid,code = packet.unpack("IHH",pkt)
    logging.debug(u"cmdKickUser uid = %d pid = %d reson = %d",uid,pid,code)
    clientByPid = ClientFactory.instance.getProtocol(pid)
    if clientByPid:
        clientUid = clientByPid.getUid()
        if uid == clientUid:
            clientByPid.sendPacket(ClientBuild.kickUser(code))
            #clientByPid.abort()
        else:
            logging.warn(u"cmdKickUser uid=%d and pid=%d client is None",uid,pid)
    else:
        logging.warn("ClientFactory not found client uid=%d pid=%d",uid,pid)

__cmdTable = {
        Const.CMD_KEEP_ALIVE                         : cmdKeepAlive,
        Const.CMD_CT2GATE_LOGINSUC                   : cmdLoginCenterSuc,
        Const.CMD_CT2GATE_GAME_UPDATE                : cmdGameUpdate,
        Const.CMD_CT2GATE_TRANS_PKT                  : cmdTransPkt,
        Const.CMD_CT2GATE_KICK_USER                  : cmdKickUser
    }

def parse(center,cmd,pkt):
    if cmd == Const.CMD_KEEP_ALIVE:
        return
    func = __cmdTable.get(cmd)
    if not func:
        logging.error(u"cmd = %d not found",cmd);
    func(center,pkt)

if __name__ == "__main__":
    pass